﻿namespace TheCodeWerks.Advertising
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using System.Windows;
	using System.Windows.Controls;
	using Microsoft.Advertising.Mobile.UI;

	public class PubCenterProvider : IAdvertisingProvider
	{
		private Dictionary<AdUnit, AdControl> adcontrols = new Dictionary<AdUnit, AdControl>(5);

		public AdvertisingConfiguration Config { get; set; }

		public AdProvider Provider { get { return AdProvider.PubCenter; } }

		public FrameworkElement CreateControl()
		{
			var control = new Grid();
			var settings = Config.Providers[Provider];
			var user = Config.User;

			AdControl.TestMode = Config.TestMode;

			if (user.Birthdate.HasValue)
			{
				AdControl.BirthDate = new BirthDate((uint)user.Birthdate.Value.Year, (uint)user.Birthdate.Value.Month);
			}

			AdControl.Gender = (Microsoft.Advertising.Mobile.UI.Gender)user.Gender;
			AdControl.PostalCode = user.Zip;
			AdControl.Country = user.CountryCode;

			if (user.Income.HasValue)
			{
				if (user.Income.Value < 15000)
					AdControl.IncomeRange = IncomeRange.A;
				else if (user.Income.Value < 25000)
					AdControl.IncomeRange = IncomeRange.B;
				else if (user.Income.Value < 50000)
					AdControl.IncomeRange = IncomeRange.C;
				else if (user.Income.Value < 75000)
					AdControl.IncomeRange = IncomeRange.D;
				else if (user.Income.Value < 100000)
					AdControl.IncomeRange = IncomeRange.E;
				else if (user.Income.Value < 125000)
					AdControl.IncomeRange = IncomeRange.F;
				else if (user.Income.Value < 150000)
					AdControl.IncomeRange = IncomeRange.G;
				else
					AdControl.IncomeRange = IncomeRange.H;
			}

			return control;
		}

		public void ConfigureControl(FrameworkElement element, AdUnit unit)
		{
			AdControl control;
			var user = Config.User;
			var settings = Config.Providers[Provider];

			if (!adcontrols.ContainsKey(unit))
			{
				control = new AdControl();
				control.ApplicationId = settings.Id;
				control.AdUnitId = unit.Id;
				control.RotationEnabled = false;				

				control.NewAd += (source, args) =>
				{
					((AdEvents)element.Tag).AdReady();
				};

				control.AdControlError += (source, args) =>
				{
					((AdEvents)element.Tag).NotFilled();
				};

				if (user.Latitude.HasValue && user.Longitude.HasValue)
				{
					control.Location = new Location(user.Latitude.Value, user.Longitude.Value);
				}

				if (!string.IsNullOrEmpty(unit.Type))
				{
					try
					{
						control.AdModel = (AdModel)Enum.Parse(typeof(AdModel), unit.Type, true);
					}
					catch { }
				}

				var keywords = new List<string>(Config.DefaultKeywords.Count + unit.Keywords.Count);
				keywords.AddRange(Config.DefaultKeywords);
				keywords.AddRange(unit.Keywords);

				control.AdSelectionKeywords = string.Join(",", keywords.ToArray());

				adcontrols.Add(unit, control);
			}
			else control = adcontrols[unit];

			var grid = (Grid)element;
			grid.Children.Clear();
			grid.Children.Add(control);
		}

		public void RequestNext(FrameworkElement element)
		{
			((AdControl)((Grid)element).Children[0]).RequestNextAd();
		}
	}
}
